<?php

namespace app\admin\service;

use app\admin\dao\PromotionGroupRoiConsumeDao;
use app\admin\model\UM as UM;
use app\common\model\PromotionGroupRoiConsume;
use ShidianUtil\Utils\ArrayUtil;
use ShidianUtil\Utils\StringUtil;
use ShidianUtil\Utils\TimeUtil;
use think\exception\ValidateException;

/**
 * Class GroupRoiConsumeInfoService
 * @package app\admin\service
 */
class GroupRoiConsumeInfoService
{
    // dao
    private PromotionGroupRoiConsumeDao $promotionGroupRoiConsumeDao;

    public function __construct(PromotionGroupRoiConsumeDao $promotionGroupRoiConsumeDao)
    {
        $this->promotionGroupRoiConsumeDao = $promotionGroupRoiConsumeDao;
    }

    /**
     * @param UM|null $user
     * @param $promotionGroupId
     * @param $time
     * @return array
     */
    public function index(?UM $user, $promotionGroupId, $time): array
    {
        $monthStartTime = TimeUtil::stamp(TimeUtil::MONTH, $time);
        $monthEndTime = TimeUtil::stamp(TimeUtil::MONTH, $monthStartTime, false);

        $startTime = TimeUtil::stamp(TimeUtil::WEEK, $monthStartTime);
        $endTime = TimeUtil::stamp(TimeUtil::WEEK, $monthEndTime, false);

        $timeList = TimeUtil::timeList(TimeUtil::DAY, $startTime, $endTime);

        $collection = $this->promotionGroupRoiConsumeDao->select($promotionGroupId, $startTime, $endTime);

        $data = [];

        foreach ($timeList as $index => $item) {
            $date = date('Y-m-d', $item['s_time']);

            $temp = ArrayUtil::find($collection->all(), function (PromotionGroupRoiConsume $consume) use ($date) {
                return StringUtil::equals($consume->date, $date);
            });

            if (!$temp) {
                $temp = new PromotionGroupRoiConsume([
                    'promotionGroupId' => $promotionGroupId,
                    'date' => $date,
                    'value' => '',
                ]);
            }
            $temp->time = strtotime($temp->date);

            $i = floor($index / 7);

            if (!isset($data[$i])) {
                $data[$i] = [];
            }
            $data[$i][] = $temp;
        }

        return $data;
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  PromotionGroupRoiConsume
     */
    public function add(UM $user, array $params): void
    {
        $data = $params['data'] ?? [];

        // 检查参数
        foreach ($data as $sub) {
            foreach ($sub as $item) {
                $promotionGroupId = $item['promotionGroupId'] ?? null;
                $date = $item['date'] ?? null;
                $value = $item['value'] ?? null;

                if (!is_id($promotionGroupId)) {
                    throw new ValidateException('小组ID不正确');
                }
                if (is_blank($date)) {
                    throw new ValidateException('日期不正确');
                }
                if (!is_blank($value) && !is_numeric($value)) {
                    throw new ValidateException($item['date'] . '请输入正确的数字');
                }
            }
        }

        //保存&删除
        foreach ($data as $sub) {
            foreach ($sub as $item) {

                $promotionGroupId = $item['promotionGroupId'] ?? null;
                $date = $item['date'] ?? null;
                $value = $item['value'] ?? null;

                if (is_blank($value)) {
                    // 删除
                    $this->promotionGroupRoiConsumeDao->deleteByUK($promotionGroupId, $date);
                } else {
                    // 保存
                    $this->promotionGroupRoiConsumeDao->replaceActive($item);
                }
            }
        }
    }

}
